﻿using FastCloud.Base;
using FastCloud.Base.Config;
using FastCloud.Base.DB;
using FastCloud.Base.Entity;
using FastIOC.Attribute;
using FastUtil.Convert;
using FastUtil.Data;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.Base
{
    [Route("frame/base/attach")]
    public class AttachController : BaseController
    {

        [AutoWired]
        private DB_Attach DB_Attach;

        [HttpPost, Route("upload")]
        public ActionResult Upload()
        {
            IFormFileCollection FileList = Request.Form.Files;
            if (FileList != null && FileList.Count > 0)
            {
                String GroupGuid = ValueConvert.ConvertToString(GetFormParam("GroupGuid"));
                String GroupType = ValueConvert.ConvertToString(GetFormParam("GroupType"));
                List<string> AttachGuidList = new List<string>();
                for (int i = 0; i < FileList.Count; i++)
                {
                    FormFile File = (FormFile)FileList[i];
                    Frame_AttachInfo AttachInfo = new Frame_AttachInfo();
                    AttachInfo.AttachFileName = File.FileName;
                    AttachInfo.ContentType = File.ContentType;
                    AttachInfo.FileSize = ValueConvert.ConvertToInt32(File.Length / 1024);
                    AttachInfo.GroupGuid = GroupGuid;
                    AttachInfo.GroupType = GroupType;
                    string FilePath = CloudUtil.GetAttachUploadPath("CommonUpload") + "/" + GenerateUtil.GenerateUnitGuid() + System.IO.Path.GetExtension(File.FileName);
                    using (FileStream fstream = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        File.CopyTo(fstream);
                    }
                    byte[] FileContent = StructConvert.ConvertFile2Byte(FilePath);
                    DB_Attach.InsertAttach(AttachInfo, FileContent);
                    AttachGuidList.Add(AttachInfo.AttachGuid);
                }
                return Result.OK(AttachGuidList);
            }
            else
            {
                return Result.Error("未选择文件");
            }
        }

        [HttpPost, Route("uploadKeepOneAttach")]
        public ActionResult UploadKeepOneAttach()
        {
            IFormFileCollection FileList = Request.Form.Files;
            if (FileList != null && FileList.Count > 0)
            {
                String AttachGuid = ValueConvert.ConvertToString(GetFormParam("AttachGuid"));
                String GroupGuid = ValueConvert.ConvertToString(GetFormParam("GroupGuid"));
                String GroupType = ValueConvert.ConvertToString(GetFormParam("GroupType"));
                List<string> AttachGuidList = new List<string>();
                if (FileList.Count==1)
                {
                    FormFile File = (FormFile)FileList[0];
                    Frame_AttachInfo AttachInfo = new Frame_AttachInfo();
                    AttachInfo.AttachGuid = AttachGuid;
                    AttachInfo.AttachFileName = File.FileName;
                    AttachInfo.ContentType = File.ContentType;
                    AttachInfo.FileSize = ValueConvert.ConvertToInt32(File.Length / 1024);
                    AttachInfo.GroupGuid = GroupGuid;
                    AttachInfo.GroupType = GroupType;
                    string FilePath = CloudUtil.GetAttachUploadPath("CommonUpload") + "/" + GenerateUtil.GenerateUnitGuid() + System.IO.Path.GetExtension(File.FileName);
                    using (FileStream fstream = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        File.CopyTo(fstream);
                    }
                    byte[] FileContent = StructConvert.ConvertFile2Byte(FilePath);
                    DB_Attach.DeleteAttach(AttachGuid);
                    DB_Attach.InsertAttach(AttachInfo, FileContent);
                    return Result.OK();
                }
                else
                {
                    return Result.Error("请选择一个文件");
                }
            }
            else
            {
                return Result.Error("未选择文件");
            }
        }


        [HttpPost, Route("delete")]
        public ActionResult Delete()
        {
            string AttachGuid = ValueConvert.ConvertToString(GetFormParam("AttachGuid"));
            DB_Attach.DeleteAttach(AttachGuid);
            return Result.OK();
        }

        [HttpPost, Route("attachlist")]
        public ActionResult AttachList()
        {
            string GroupGuid = ValueConvert.ConvertToString(GetFormParam("GroupGuid"));
            string GroupType = ValueConvert.ConvertToString(GetFormParam("GroupType"));
            List<Frame_AttachInfo> AttachList = DB_Attach.SelectAttachByGroupGuid(GroupGuid, GroupType);
            return Result.OK(AttachList);
        }

        [HttpGet, Route("readfile")]
        public ActionResult ReadFile()
        {
            string AttachGuid = ValueConvert.ConvertToString(GetRequestParam("AttachGuid"));
            Response.Redirect(DB_Attach.GetAttachUrl(AttachGuid));
            return Result.OK();
        }

    }
}
